<%
# View which displays the list of updates in an incident'.  
#
# Author::      Eli Fox-Epstein, efoxepstein@wesleyan.edu
# Author::      Dimitar Gochev, dimitar.gochev@trincoll.edu
# Copyright::   Humanitarian FOSS Project (http://www.hfoss.org), Copyright (C) 2009.
# License::     http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License (LGPL)
%>

<% content_for :title, title('Updates', @incident.name, 'Incidents', @instance.short_name)%>
<% content_for :javascripts, javascript_include_tag('jquery','updates')%>
<% content_for :body do %>
	<%= pretty_button_if(@incident.destroyable_by?(@current_user), :delete_incident, instance_incident_path(@instance, @incident), 'Delete Incident', :method => :delete, :confirm => 'Do you really want to delete?') %>
	<%= pretty_button_if(@incident.updatable_by?(@current_user), :edit_incident, edit_instance_incident_path(@instance, @incident), 'Edit Incident') %>
	<%= pretty_button_if(@incident.updatable_by?(@current_user), :close_incident, instance_incident_close_path(@instance,@incident), @incident.closed_at ? 'Reopen Incident' : 'Close Incident') %>
	<%= pretty_button_if((Update.creatable_by?(@current_user) and @instance.viewable_by?(@current_user)), :add_update, new_instance_incident_update_path(@instance, @incident), 'Add Update')%>
	
<%= flashes %>

	<div id="wrap">
		<h1 id="incident-name">
			<%= @incident.name%>
			<span id="incident-description"><%= @incident.description%></span>
		</h1>
		<% if @incident.closed_at? %>
		<h2 id="incident-closed">
			Incident Closed
		</h2>
		<% end %>
		<div id="view-options">
			<h3>Filters</h3>
	    <% form_tag instance_incident_updates_path(@instance, @incident), :method => :get do %>
	        <!-- <%= select_tag(:detail_level, options_for_select(['Concise', 'Detailed'], @detail_level)) %> -->
	        
	        <% # This should be made to be more efficient:
	            groups = []
	            @incident.updates.each {|u|
	                groups += u.relevant_groups.reject{|g| groups.include?(g) }

	                unless u.issuing_type == :user || groups.include?(u.issuing_group)
	                    groups << u.issuing_group 
	                end
	            }
	            tags = []
	            @updates.each {|u| tags += u.tags.reject{|t| tags.include?(t) }}
	        %>
	        
	        <%= text_field_tag :search, (params[:search] || 'Search Keywords'), :size => 20, :class=>(!params[:search] || params[:search] == '' ? 'blank':'') %>
	        
	        <select name="filters[relevant_groups][id]" id="relevant_groups_filter">
	            <%  selected_group = case @group_filter.class.name
                        when String.name then 'mine'
                        when Group.name then @group_filter.id
                        else nil
                    end
	            %>
	            <%= options_for_select([['All Groups', nil],['My Groups', 'mine']] + groups.map{|g|[g.name, g.id]}, selected_group) %>
	        </select>
	        <select name="filters[tags][id]" id="tags_filter">
	            <%= options_for_select([['All Tags', nil]] + tags.map{|g|[g.name, g.id]}, (@tag_filter && @tag_filter.id)) %>
	        </select>    				
			<%= submit_tag "Go", :name => nil %>
	    <% end %>
		</div>
		<% unless @incident.updates.empty? %>
			<ul id="updates-list">
			<% @updates.each do |u| %>
				<li>
					<%= link_to u.title, instance_incident_update_url(@instance, @incident, u), :class => 'update-title' %>
					<span class="update-meta">by 
					    <%= issuer_link(u)%>, <%= 'updated ' if u.updated_at > u.created_at %> <%= time_mdy u.updated_at %></span>
					<p class="update-text">
						<%=simple_format(h(u.text)).split('</p>')[0][0..256] %><%='...' if u.text.length > 256%>
					</p>
				<% unless u.attachments.empty? %>
					<div class="attachments">
						<strong>Attached Files:</strong>
						<ul>
						<% u.attachments.each do |f| %>
							<li><%=link_to f.attach.original_filename, f.attach.url%></li>
						<% end %>
						</ul>
					</div>				
				<% end
				   unless u.relevant_groups.empty? %>
					<div class="relevant-groups">
						<em>Relevant Groups:</em>
						<ul>
						<% u.relevant_groups.each do |g| %>
							<li><%=link_to g.name, instance_group_type_group_path(@instance, g.group_type, g) %></li>		
						<% end %>	
						</ul>
					</div>
				<% end 
				   unless u.tags.empty? %>
					<div class="tags">
						<em>Tags:</em>
						<ul>
						<% u.tags.each do |t| %>
							<li><%=link_to t.name, instance_tag_path(@instance, t) %></li>
						<% end %>
						</ul>
					</div>
				<% end %>
				</li>
			<% end %>
			</ul>
		<% else %>
			<h3 class="centered">This incident has no updates. <%=link_to 'Click here to update', new_instance_incident_update_path(@instance, @incident)%>.</h3>
		<% end %>
		<%= will_paginate @updates %>
	</div>
<% end %>
